/*
 * ShoppingCartGoodsListItem.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.model.IShoppingCartModel;
import com.yidiandao.business.data.model.ShoppingCartModel;
import com.yidiandao.business.manager.ShoppingCartManager;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.ShoppingCartGoodsHolder;
import com.yidiandao.business.ui.widget.view.GoodsCountMenu;
import com.yidiandao.business.utils.UmengUtils;

/**
 * 购物车普通类型的ListItem
 */
public class ShoppingCartGoodsListItem extends DisplayListItem<ShoppingCartGoodsHolder, Goods> {

    private IShoppingCartModel mShoppingCartModel;

    public ShoppingCartGoodsListItem(Goods s) {
        super(s);
        mShoppingCartModel = new ShoppingCartModel();
    }

    @Override
    public void onShow(final Context context, final ShoppingCartGoodsHolder holder) {

        holder.refreshUI(data);

        // 购物车中商品的数量
        final int goodsNumber = data.getGoodsNumber();
        // 先设置模式,再设置数量
        holder.refreshGoodsCountMenuMode(data.isEditMode());
        // 设置数量
        holder.setGoodsPriceAndCount(data.getGoodsPrice(), goodsNumber, data.getStockNumber());

        holder.mRlytSelect.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean checked = holder.mCbSelect.isChecked();
                        // 点击选中商品
                        ShoppingCartManager.getInstance().selectSingleGoods(data, !checked);
                        // 所有商品均选择时选择店铺
                        ShoppingCartManager.getInstance().clearOrSelectShopByGoods(data);

                        // 点击时刷新ListView 以便根据 数据 切换 item checkbox的状态
                        mShoppingCartModel.postRefreshShoppingCartState(false);
                    }
                });

        holder.mGoodsCountMenu.setOnGoodsCountChangeListener(
                new GoodsCountMenu.OnGoodsCountChangeListener() {
                    @Override
                    public void onGoodsCountChanged(int count) {
                        UmengUtils.onEvent(context, R.string.umeng_categray_goods_num);
                        // 修改当前Item显示的价格与数量
                        //                holder.setGoodsPriceAndCount(data.getGoodsPrice(),
                        // count, data.getStockNumber());
                        //
                        // 同时 保存商品数量
                        //                data.setGoodsNumber(count);
                        //
                        //                // 切换数量时刷新ListView 以便 刷新 item 单个商品的总的价格
                        //                mShoppingCartModel.postRefreshShoppingCartState(false);

                        // 回掉商品的数量发生改变，请求服务器，更新当前店铺订单信息
                        // 替换店铺的商品列表
                        // 记录当前商品的数量，使用一个新的变量，不适用goodsNumber
                        data.setNumberHelper(count);
                        ShoppingCartManager.getInstance().notifyGoodsNumberChanged(data);
                    }
                });

        holder.mIvGoodsImage.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!data.isEditMode()) {
                            GoodsDetailActivity.actionStart((Activity) context, data.getGoodsId());
                        }
                    }
                });
    }

    @Override
    public String getDisplayItemTag() {
        return ShoppingCartGoodsListItem.class.getSimpleName();
    }

    @Override
    public Class<ShoppingCartGoodsHolder> getViewHolderClazz() {
        return ShoppingCartGoodsHolder.class;
    }
}
